function theAjax(data, evtAfter){
	$.ajax({
		url: sAjaxURL,
		type: 'POST',
		cache: false,
		data: data,
		success: function(response) {
			evtAfter(response);
		}
	});
}

(function($){
    var bSubmit = true;
    $.fn.reverse = [].reverse;
    $.fn.validate = function(){
        bSubmit = true;
        this.find('p.validate-error').remove();
        var aFields = this.find('input[type=text], input[type=hidden], textarea').reverse();
        aFields.each(function(index, field){
            var validate = field.getAttribute('data-validate');
            if(validate!==null){
                var aValidate = validate.split('/');
                for(i=0;i<aValidate.length;i++)
                {
                    if(aValidate[i].indexOf('length')!=-1){
                        var aFunc = aValidate[i].replace('length(','').replace(')','').split(',');
                        eval("length('" + this.getAttribute('id') + "','" + aFunc[0] + "','" + aFunc[1] + "')");
                    } else {
                        eval(aValidate[i]+"('" + this.getAttribute('id') + "')");    
                    }
                }
            }
        });
        return bSubmit;
    };    
    function renderMessage(control){
        closestLabel = control.closest('div.form-group').find('label');
        closestPValidateError = closestLabel.find('p.validate-error');
        if(closestPValidateError.length==0){
            closestLabel.append('<p class="validate-error">'+control.attr('data-validate-message')+'</p>');    
        } else {
            closestPValidateError.html(control.attr('data-validate-message'));
        }
        control.focus();
        bSubmit = false;
    }
    function required(item){
        control = $('#'+item);
        if(control.val().trim()===''){
            renderMessage(control);
        }
    }

    function alphabet(item){
        control = $('#'+item);
        regex = /^[a-zA-Z ]+$/;
        if(control.val().match(regex)==null){
            renderMessage(control);
        }
    }
    function email(item){
        control = $('#'+item);
        regex = /^[a-zA-Z0-9]+@[a-zA-Z0-9]+\.[a-zA-Z]+$/;
        if(control.val().match(regex)==null){
            renderMessage(control);
        }
    }
    function numeric(item){
        control = $('#'+item);
        regex = /^[0-9]+$/;
        if(control.val().match(regex)==null){
            renderMessage(control);
        }
    }
    function length(item, mi, ma){
        control = $('#'+item);
        if(control.val().length < mi || control.val().length > ma){
            renderMessage(control);
        }
    }
}(jQuery));